home *** CD-ROM | disk | FTP | other *** search
/ Commodore Free 13 / Commodore_Free_Issue_13_2007_Commodore_Computer_Club.d64 / t.hex files 8 < prev    next >
File List  |  2023-02-26  |  11KB  |  360 lines

  1. uHexFiles part 8
  2. By Jason Kelk
  3. http://www.oldschool-gaming.com
  4.  
  5. Okay, so last issue we were looking at
  6. $D018 & how it controls where our
  7. screen & character set data are held in
  8. the C64's memory & most of you (I hope)
  9. will have noticed that the highest
  10. place I said we could put a font was
  11. $3800 & the highest place for a screen
  12. $3C00, yes? Simiarly, because sprite
  13. data pointers can only have a maximum
  14. value of $FF & each sprite is $40 bytes
  15. long, the last sprite is at $3FC0. This
  16. is because, the VIC-II chip arranges
  17. memory into four chunks of 16K. On
  18. powerup, the C64 is pointing to video
  19. bank 0, which runs from $0000 to $3FFF,
  20. & at the screen at $0400 & a font at
  21. $1000. This, incidentally, is why our
  22. examples have all had music sitting at
  23. $1000, the VIC-II can't 'see' this RAM
  24. under there for sprites, characters or
  25. screen data because a copy of the ROM
  26. character set is there, $1000 to $17FF
  27. for the upper case font & $1800 to
  28. $2000 for the lower.
  29.  
  30. The easiest way to imagine it is with 2
  31. pieces of paper. The first has a piece
  32. of code written on it & is placed on a
  33. desk whilst the second has a picture
  34. drawn on it & is held a small distance
  35. above the first. We, pretending to be
  36. the processor here, can look at the 1st
  37. piece of paper by putting our head
  38. underneath the second, but the VIC-II
  39. looks straight down & from this angle
  40. the first is totally obscured. This
  41. technique is called 'shadowing' and,
  42. although it appears to be adding a
  43. limitation to how we use memory, it
  44. actually makes life easier for us in
  45. that we can still actually use the
  46. memory in some way rather than it being
  47. locked off for the characters. We'll
  48. come across more examples of shadowing
  49. in a different form in a later article.
  50.  
  51. But back to our initial line of
  52. thought, how can we use one of the
  53. other three blocks of 16K for our
  54. graphics? After all, if we use a bitmap
  55. picture (more on those a little later)
  56. we need about 9K of space for it,
  57. there is only a limited space in bank
  58. 0, what with $0000 to $0400 & $1000 to
  59. $2000 being unavailable to us.
  60.  
  61. This is where $DD00 comes in to play.
  62. One of $DD00's jobs is handling this
  63. very problem, the lowest two bits are
  64. used to point VIC-II at the correct
  65. place. If we just put a value of $03
  66. into $DD00 nothing happens because,
  67. oddly, $DD00 actually refers to the
  68. first bank as $03 & the last as $00. So
  69. if we want to use bank 1 ($4000-$7FFF)
  70. we can do so by setting $DD00 with $02.
  71.  
  72. Changing into bank 1 is the equivilent
  73. of adding $4000 (16384) to all of our
  74. character, screen 6 sprite data
  75. pointers. If we just change into bank 1
  76. with no other changes made to the
  77. VIC-II we end up looking at a screen at
  78. $4400 & a font at $5000. Since there is
  79. no shadow of the ROM font in bank 1
  80. that means we just see a mess. And more
  81. importantly, we have the entire 16K to
  82. ourselves! And simliar rules apply to
  83. banks 2 & 3, except that bank 2 starts
  84. at $8000 & has a second copy of the ROM
  85. font at $9000 to $9FFF & bank 3 starts
  86. at $C000 and has the video chip sitting
  87. at $D000 to $E000. We can write to this
  88. memory, but it requires a little
  89. trickery and, since using it is a lot
  90. more complex, I won't cover this until
  91. later. But bank 1 is the most commonly
  92. used bank for graphics since it's all
  93. ours with absolutely no strings
  94. attached by the C64, so for now we're
  95. going to frolic in this new pasture &
  96. not worry about the rest.
  97.  
  98. Now we've got a play space, time to
  99. introduce something new to fill it with
  100. in the form of bitmaps. Remember I
  101. mentioned them last time as well?
  102. Right, first off, there are a number of
  103. different editors out there, all with
  104. different memory layouts. During the
  105. planning for these articles, there was
  106. some discussion about which would be
  107. best to use for our purposes & we've
  108. decided to use the format set down by
  109. the editor Vidcom. In general a bitmap
  110. is split into two parts, the actual
  111. picture (which is 8,000 bytes of data,
  112. since 40 characters across times 25
  113. down times 8 bytes a character comes to
  114. 8,000) and we're only allowed to put
  115. this in either $4000 or $6000 in this
  116. bank (and the same rule applies to the
  117. other banks, the bitmap can only start
  118. at a multiple of $2000).
  119.  
  120. The second part is the colors, with a
  121. monocolor bitmap we get two colors
  122. every 8x8 pixel square of the picture &
  123. these can be represented by the two
  124. halves of a byte; $F4 for example will
  125. be light grey ($F) & purple ($4). This
  126. means we need 1,000 bytes of color for
  127. a monocolor bitmap. Multicolor is
  128. different, we get the same system for
  129. defining 2 of the colours, but the
  130. $D800 colour map is also available as
  131. is the background colour, since multi-
  132. color bitmaps work in the same way as
  133. multicolor characters with two bits
  134. working together to make a color value
  135. of $0 to $3. This means we need a total
  136. 2,000 bytes of color data, one for the
  137. screen & the other for $D800. Vidcom
  138. arranges its files like this:
  139.  
  140. $5800 to $5BE7 - color data for $D800
  141. onwards.
  142.  
  143. $5C00 to $5FE7 - color data for
  144. wherever the screen is.
  145.  
  146. $6000 to $7F3F - bitmap.
  147.  
  148. We're going to take advantage of this.
  149. Because any of the sixteen screens in
  150. bank 1 can be used as our screen we're
  151. not going to bother copying the data
  152. from $5C00 to $5FE7, oh no. We're going
  153. to tell the VIC-II to look there for
  154. it's screen, & by a coincidence... oh
  155. look, here's our color data! Now, if
  156. you download & unzip the sample data
  157. there is an example picture included
  158. called tropique.prg & a piece of source
  159. that I want you to open called
  160. pic_show.asm. It should look like this:
  161.  
  162.   .incbin tropique.prg
  163.  
  164.   *= $0900
  165.  
  166. ; Black border & screen colors
  167.   lda #$00
  168.   sta $d020
  169.   sta $d021
  170.  
  171. ; Set VIC-II to bank 1
  172.   lda #$02
  173.   sta $dd00
  174.  
  175. ; Turn on bitmap mode
  176.   lda #$3b
  177.   sta $d011
  178.  
  179. ; Turn on multicolor mode
  180.   lda #$18
  181.   sta $d016
  182.  
  183. ; Point the screen at $5C00 & the font
  184.    at $6000
  185.   lda #$78
  186.   sta $d018
  187.  
  188. ; Copy from $5800-$5BE7 to $D800-$dBE7
  189.   ldx #$00
  190. copycol  lda $5800,x
  191.   sta $d800,x
  192.   lda $5900,x
  193.   sta $d900,x
  194.   lda $5a00,x
  195.   sta $da00,x
  196.   lda $5ae8,x
  197.   sta $dae8,x
  198.   inx
  199.   bne copycol
  200.  
  201. ; Stop but don't exit to BASIC
  202. loop  jmp loop
  203.  
  204.  
  205. Start it up & if it's typed correctly,
  206. a nice picture of a girl & some stone
  207. columns should appear! There's also a
  208. file in the archive called pic_demo.asm
  209. and, as the filename might suggest,
  210. this is a demo based on the previous
  211. code we've looked at & the picture
  212. viewer. This source isn't documented,
  213. but the majority of it is code we've
  214. already covered quite extensively so
  215. you should feel pretty comfortable with
  216. it - but there have been a few alter-
  217. ations made so they need a little
  218. explanation; one change makes the
  219. scroller faster & therefore more read-
  220. able, the other extends the length of
  221. the message itself so it's no longer
  222. limited to 256 bytes.
  223.  
  224. Lets look at how the scroll is sped up
  225. first; open the pic_show.asm source &
  226. look for the label scrlloop it should
  227. be line 127) & there is an LDY #$00
  228. just before it; since the Y register
  229. isn't used during the scroll movement
  230. routine, it's being put to service as a
  231. counter to call that code more than
  232. once per frame. If you page onwards a
  233. little to the label dontmove, here's
  234. this;
  235.  iny
  236.  cpy #$03
  237.  bne scrlloop
  238.  
  239. And that's what speeds the scroller up;
  240. the routine is called, Y gets increm-
  241. ented & we go back to scrlloop until Y
  242. reaches 3. Changing the value can speed
  243. up or slow down the scroller accord-
  244. ingly, so it's worth having a little
  245. play to see how it looks, on average
  246. moving two to three pixels a frame
  247. tends to be readable when the text is
  248. the size we're using here.
  249.  
  250. The other modification is a little more
  251. involved & before I start explaining
  252. how it works a little disclaimer is
  253. probably in order; the technique below
  254. is called 'self modifying code' & is,
  255. generally speaking, considered bad
  256. practice by just about every prog-
  257. ramming course, teacher & book.
  258.  
  259. However, it's very effective & faster
  260. to use most of the time & since I use
  261. it personally in preference to the more
  262. fiddly 'proper' methods, it's covered
  263. here.
  264.  
  265. Previously, we've been using a label
  266. called messcount to indicate where we
  267. are in the text, that has been totally
  268. removed for this code. Instead we have
  269. this routine after the loop to shift
  270. all the characters to the left:
  271.  
  272. messread lda message
  273.  
  274.   bne textok
  275.   jsr reset
  276.   jmp messread
  277.  
  278. textok  sta $5c27
  279.  
  280.   inc messread+$01
  281.   bne nohibyte
  282.   inc messread+$02
  283.  
  284. So, looking at this new routine it
  285. reads from the label message (which is
  286. where the scroll text is stored, at
  287. $2800 in memory for this code), checks
  288. it's not a value of $00 & puts the new
  289. character on the screen. If the accum-
  290. ulator does contain $00, a subroutine
  291. called reset is called & the code jumps
  292. back to the LDA to get another byte.
  293.  
  294. Now, some of you are thinking 'hang on,
  295. that just reads the same byte over &
  296. over again' & on it's own you'd be
  297. right, the two INC commands after
  298. textok are what nudges the LDA on so it
  299. reads the text.
  300.  
  301. How does that work, then? Well, if we
  302. look into memory where the LDA message
  303. is stored, it will look like this; $AD,
  304. $00, $28. The first byte $AD is the
  305. actual LDA command, while the second &
  306. third bytes are the address, in this
  307. case pointing to $2800 since the C64
  308. insists on storing the lower byte of
  309. the address first. The two INCs change
  310. the address, the first one changes the
  311. lower byte (the one that starts as $00
  312. in this case) upwards so the LDA itself
  313. steps through memory a byte at a time,
  314. while the second INC only takes effect
  315. at the point when that byte reaches $00
  316. again; it bumps the higher byte of the
  317. address up so the scroller can go from
  318. $28ff to $2900. Finally, we need a look
  319. at reset (near the end of the code) as
  320. well:
  321.  
  322. reset  lda #message
  323.   sta messread+$02
  324.   rts
  325.  
  326. This subroutine simply resets messread
  327. so that it points to the first byte of
  328. the text, the first LDA/STA pair sets
  329. the lower byte of the address, the
  330. second LDA/STA sets the higher, so the
  331. code at messread gets changed to $AD,
  332. $00, $28 regardless of what it was
  333. previously.
  334.  
  335. This subroutine is also called a little
  336. before the CLI in the setup code to
  337. make sure that the program behaves if
  338. someone stops & restarts it. Without
  339. that call in the setup the text would
  340. continue where it left off!
  341.  
  342. Have fun playing with the code,
  343. changing the speed of the scroller & so
  344. forth & generally seeing how things
  345. work. For the next installment we're
  346. going to start a whole new project. As
  347. usual, if you have any questions,
  348. contact me & I'll see what I can do
  349. but, for now, goodbye!
  350.  
  351. The source code for the routines above
  352. can be downloaded at:
  353. www.oldschool-gaming.com/files/c64/
  354.  hex_files/part_8_files.zip
  355.  
  356. Commodore Free would like to thank
  357. Jason for permitting the reprinting of
  358. the article
  359.  
  360.